package com.hotchpotch.util;

import android.content.Context;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;


import com.hotchpotch.R;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/6/12 0012.
 */

public class TextViewUtil {
    public static void setDiffTextColordf262c(Context context, TextView textView, String string, int start, int end) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(string);
        stringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colordf262c)), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(stringBuilder);
    }

    public static void setDiffTextColor(Context context, int color, TextView textView, String string, int start, int end) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(string);
        stringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(color)), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(stringBuilder);
    }

    /**
     * 设置不要字体大小
     * @param context
     * @param textView
     * @param content
     * @param start
     * @param end
     */
    public static void setDiddTextSize(Context context, TextView textView, String content, int start, int end) {
        SpannableString textSpan = new SpannableString(content);
        textSpan.setSpan(new AbsoluteSizeSpan(90), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(textSpan);
    }

    /**
     * 替换指定位置字符串为*
     *
     * @param string
     * @param where  要替换的字符串的最后位置
     * @return
     */
    public static String getReplaceStr(String string, int where) {
        String st = "";
        for (int i = 0; i < where; i++) {
            st = new StringBuilder(st).append("*").toString();
        }
        String newStr = string.replace(string.substring(0, where), st);
        return newStr;
    }

    /**
     * 以点分割字符串
     *
     * @param string
     */
    public static String[] doSplit(String string) {
        double pri = Double.parseDouble(string);
        String result = new DecimalFormat("#.00").format(pri);
//        Logg.e("分割字符串", "result = " + result);
        String[] strings = result.split("\\.");
//        Logg.e("字符串分割", "大小：" + strings.length);
        return strings;
    }

    /**
     * 得到小数位是两位的价格
     *
     * @param price
     * @return
     */
    public static String twoDecimalPri(String price) {
        double pri = Double.parseDouble(price);
        String result = new DecimalFormat("#.00").format(pri);
        return result;
    }

    /**
     * 字体加粗
     */
    public static void setBold(TextView... textViews) {
        for (TextView tv : textViews) {
            tv.getPaint().setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
        }
    }
}
